﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EpiPoker.DataAccess
{
    /// <summary>
    /// Stored Procedures for T_User Table
    /// </summary>
    public class User
    {
        #region UpdateDBB
        /// <summary>
        /// Create User
        /// </summary>
        /// <param name="user">User object to create</param>
        /// <returns>true if goes well</returns>
        public static bool CreateUser(T_User user)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                user.DateRegistration = DateTime.Now;
                bdd.AddToT_User(user);
                bdd.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                //throw ex;
                return false;
            }
        }

        /// <summary>
        /// Delete user
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>true if deleted</returns>
        public static bool DeleteUser(long id)
        {
            try
            {
                T_User u = GetUser(id);
                u.IsActive = false;
                UpdateUser(u);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// Update the user
        /// </summary>
        /// <param name="user">the user object replacing the old one</param>
        /// <returns>true if updated</returns>
        public static bool UpdateUser(T_User user)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                T_User userDB = bdd.T_User.Where(x => x.IdUser == user.IdUser).FirstOrDefault();

                if (userDB != null)
                {
                    userDB.IsActive = user.IsActive;
                    userDB.DateUpdate = DateTime.Now;
                    userDB.IdTable = user.IdTable;
                    userDB.FirstName = user.FirstName;
                    userDB.LastName = user.LastName;
                    userDB.AvatarUrl = user.AvatarUrl;
                    userDB.NumPlace = user.NumPlace;

                    bdd.SaveChanges();
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region GetFromDBB
        /// <summary>
        /// Get a certain user
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>user object if goes well, else null</returns>
        public static T_User GetUser(long id)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                return bdd.T_User.Where(x => x.IdUser == id).FirstOrDefault();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Complete user list
        /// </summary>
        /// <returns>list of users else an empty user list</returns>
        public static List<T_User> GetListUser()
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                return bdd.T_User.ToList();
            }
            catch (Exception ex)
            {
                return new List<T_User>();
            }
        }

        /// <summary>
        /// Get a certain user
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>user object if goes well, else null</returns>
        public static T_User GetUserWithTableAndSeat(long idTable, int numPlace)
        {
            try
            {
                fbPokerEntities bdd = new fbPokerEntities();
                return bdd.T_User.Where(x => x.IdTable == idTable).Where(x => x.NumPlace == numPlace).FirstOrDefault();
            }
            catch (Exception ex)
            {
                Exception Excep = new Exception(ex.Message, ex);
                throw Excep;
                //return null;
            }
        }
        #endregion
    }
}